View Javadoc
1   package org.apache.maven.surefire.junitcore;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collection;
23  import java.util.List;
24  import java.util.concurrent.ConcurrentLinkedQueue;
25  import java.util.concurrent.atomic.AtomicBoolean;
26  import org.apache.maven.surefire.report.ReportEntry;
27  import org.apache.maven.surefire.report.RunListener;
28  import org.apache.maven.surefire.report.SimpleReportEntry;
29  
30  import org.junit.runner.Description;
31  
32  /**
33   * * Represents the test-state of a testset that is run.
34   */
35  public class TestSet
36  {
37      private static final InheritableThreadLocal<TestSet> TEST_SET = new InheritableThreadLocal<TestSet>();
38  
39      private final Description testSetDescription;
40  
41      private final Collection<TestMethod> testMethods = new ConcurrentLinkedQueue<TestMethod>();
42  
43      private final AtomicBoolean played = new AtomicBoolean();
44  
45      private volatile boolean allScheduled;
46  
47      private volatile int numberOfCompletedChildren;
48  
49      // While the two parameters may seem duplicated, it is not entirely the case,
50      // since numberOfTests has the correct value from the start, while testMethods grows as method execution starts.
51  
52      private volatile int numberOfTests;
53  
54      public TestSet( Description testSetDescription )
55      {
56          this.testSetDescription = testSetDescription;
57      }
58  
59      public void replay( RunListener target )
60      {
61          if ( played.compareAndSet( false, true ) )
62          {
63              try
64              {
65                  ReportEntry report = createReportEntry( null );
66  
67                  target.testSetStarting( report );
68  
69                  long startTile = 0;
70                  long endTime = 0;
71                  for ( TestMethod testMethod : testMethods )
72                  {
73                      if ( startTile == 0 || testMethod.getStartTime() < startTile )
74                      {
75                          startTile = testMethod.getStartTime();
76                      }
77  
78                      if ( endTime == 0 || testMethod.getEndTime() > endTime )
79                      {
80                          endTime = testMethod.getEndTime();
81                      }
82  
83                      testMethod.replay( target );
84                  }
85  
86                  int elapsed = (int) ( endTime - startTile );
87  
88                  report = createReportEntry( elapsed );
89  
90                  target.testSetCompleted( report );
91              }
92              catch ( Exception e )
93              {
94                  throw new RuntimeException( e );
95              }
96          }
97      }
98  
99      public TestMethod createThreadAttachedTestMethod( ReportEntry description )
100     {
101         TestMethod testMethod = new TestMethod( description, this );
102         addTestMethod( testMethod );
103         testMethod.attachToThread();
104         return testMethod;
105     }
106 
107     private ReportEntry createReportEntry( Integer elapsed )
108     {
109         final String className = testSetDescription.getClassName();
110         final boolean isJunit3 = className == null;
111         final String classNameToUse;
112         if ( isJunit3 )
113         {
114             List<Description> children = testSetDescription.getChildren();
115             classNameToUse = children.isEmpty() ? testSetDescription.toString() : children.get( 0 ).getClassName();
116         }
117         else
118         {
119             classNameToUse = className;
120         }
121         return new SimpleReportEntry( classNameToUse, classNameToUse, elapsed );
122     }
123 
124     public void incrementTestMethodCount()
125     {
126         numberOfTests++;
127     }
128 
129     private void addTestMethod( TestMethod testMethod )
130     {
131         testMethods.add( testMethod );
132     }
133 
134     public void incrementFinishedTests( RunListener reporterManager, boolean reportImmediately )
135     {
136         numberOfCompletedChildren++;
137         if ( allScheduled && isAllTestsDone() && reportImmediately )
138         {
139             replay( reporterManager );
140         }
141     }
142 
143     public void setAllScheduled( RunListener reporterManager )
144     {
145         allScheduled = true;
146         if ( isAllTestsDone() )
147         {
148             replay( reporterManager );
149         }
150     }
151 
152     private boolean isAllTestsDone()
153     {
154         return numberOfTests == numberOfCompletedChildren;
155     }
156 
157     public void attachToThread()
158     {
159         TEST_SET.set( this );
160     }
161 
162     public static TestSet getThreadTestSet()
163     {
164         return TEST_SET.get();
165     }
166 }